package shanghaielectric_lib.Utils;

import android.annotation.SuppressLint;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by 45000460 on 2017/12/20.
 */

public class DateUtil {
    public static String getSystemTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());

        return formatter.format(curDate);
    }


    public static String getSystemTime_yyyy_MM_dd_HH() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }

    /**
     * 精确到当前小时的long型日期
     */
    public static long getCurrentSystemTime_yyyy_MM_dd_HH_long() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
        Date curDate = new Date(System.currentTimeMillis());
        String data_h = formatter.format(curDate);
        Date result = curDate;
        try {
            result = formatter.parse(data_h);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result.getTime();
    }

    /**
     * 精确到当前小时的long型日期
     */
    public static long transferStringDate_yyyy_MM_dd_HH_00_long(String stringDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:00");
        Date result = new Date();
        try {
            result = formatter.parse(stringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result.getTime();
    }

    /**
     * 精确到当前小时的long型日期
     */
    public static long transferStringDate_yyyy_MM_dd_long(String stringDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date result = new Date();
        try {
            result = formatter.parse(stringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result.getTime();
    }

    /**
     * 精确到当前天的long型日期
     */
    public static long getSystemTime_yyyy_MM_dd_long() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Date curDate = new Date(System.currentTimeMillis());
        String data_h = formatter.format(curDate);
        Date result = curDate;
        try {
            result = formatter.parse(data_h);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result.getTime();
    }

    public static String getSystemTime_yyyy_MM_dd_HH_nextDay() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH");
        Date curDate = new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000);
        return formatter.format(curDate);
    }

    public static String getSystemTime_yyyy_MM_dd() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }

    public static String getSystemTime_yyyy_MM_dd_nextDay() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000);
        return formatter.format(curDate);
    }

    public static String getSystemTime_yyyy_MM_dd_yesterday() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
        return formatter.format(curDate);
    }

    public static String getSystemTime_yyyy_MM_dd_HH_mm_ss() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }

    public static String getSystemTime_HH() {
        SimpleDateFormat formatter = new SimpleDateFormat("HH");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }

    public static Date stringToDate(String stringDate) {
        Date date = new Date();
        String string = "2016-10-24 21:59:06";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date stringToDate_yy_MM_dd(String stringDate) {
        Date date = new Date();
        String string = "2016-10-24";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = sdf.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    @SuppressLint("SimpleDateFormat")
    public static String dateToString_yy_MM_dd(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    @SuppressLint("SimpleDateFormat")
    public static String dateToString_yy_MM_dd(long date) {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(new Date(date));
    }

    @SuppressLint("SimpleDateFormat")
    public static String dateToString_HH_mm_ss(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        return dateFormat.format(date);
    }

    @SuppressLint("SimpleDateFormat")
    public static String dateToString_HH_mm_ss(long date) {
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        return dateFormat.format(new Date(date));
    }

    /**
     * 获取一个月前的日期
     *
     * @param date 传入的日期
     * @return
     */
    public static long getNextMonth(String date) {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(transferStringDate_yyyy_MM_dd_long(date)));
        calendar.add(Calendar.MONTH, 1);
        //        String monthAgo = simpleDateFormat.format(calendar.getTime());
        return calendar.getTime().getTime();
    }


    /**
     * 获得当前日期的 "日"
     */
    public static int getDayFromDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }
    /**
     * 获得当前日期的 "日"
     */
    public static int getDayFromDate(String  date) throws ParseException {
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateFormat.parse(date));
        return calendar.get(Calendar.DAY_OF_MONTH);
    }
}
